﻿using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL

{
    public class Base<T> : IBase<T> where T : class, new()
    {


        public bool Add(T model)
        {
                return DB.Client.Insertable<T>(model).ExecuteCommand() > 0;
        }



        public bool Delete(int Id)
        {
            return DB.Client.Deleteable<T>().In(Id).ExecuteCommandHasChange();
        }
        public bool DeleteList(int[] Id)
        {
            return DB.Client.Deleteable<T>().In(Id).ExecuteCommandHasChange();
        }

        public bool DeleteList(string IDlist)
        {

            var list = IDlist.Split(',');
            int[] output = Array.ConvertAll<string, int>(list, delegate (string s) { return int.Parse(s); });
            return DB.Client.Deleteable<T>().In(output).ExecuteCommandHasChange();

        }

        public bool Exists(int Id)
        {
            return DB.Client.Queryable<T>().In(Id).Any();

        }




        public T GetModel(int Id)
        {
            return DB.Client.Queryable<T>().In(Id).First();
        }

        public int GetRecordCount(string strWhere)
        {
            return DB.Client.Queryable<T>().Where(strWhere).Count();
        }

        public bool Update(T model)
        {
            return DB.Client.Updateable(model).ExecuteCommand() > 0;
        }
        public int GetMaxId()
        {
            return DB.Client.Queryable<T>().Max<int>("Id");
        }
        public List<T> GetList(string strWhere)
        {
            var page = DB.Client.Queryable<T>().Where(strWhere).OrderBy("Id desc").ToList();
            return page;
        }

        public List<T> GetListByPage(string strWhere, string orderby, int pageSize, int startIndex)
        {
            var totalCount = 0;
    
            var page = DB.Client.Queryable<T>().Where(strWhere).OrderBy(orderby).ToPageList(startIndex, pageSize, ref totalCount);
            return page;
        }
    }
}
